import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
// 引入moment
import moment from 'moment'
// 引入Vant
import Vant from 'vant'
import { Toast, Lazyload } from 'vant'
import 'vant/lib/index.css'

// 引入axios
import axios from './http/http'
import VueAxios from 'vue-axios'
const api = require('@/http/api')


Vue.use(Vant)
Vue.use(Toast)
Vue.use(Lazyload)

Vue.use(VueAxios,axios)


Vue.config.productionTip = false
// 配置axios响应拦截器
axios.interceptors.response.use(
  response => {
    // 当token过期或者不存在时，后台返回状态码202，此时页面跳转到登录页
    if (response.status === 401 || response.data.code == 202) {
      Toast.fail('登录信息已过期，请重新登录！')
      router.replace({ path: '/login' })
    }
    return response
  },
  error => {
    return Promise.reject(error)
  }
)

Vue.prototype.http = axios
Vue.prototype.$moment = moment
Vue.prototype.$api = api
Vue.prototype.$msg = Toast

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

// 移动端建议使用rem作为像素单位
// 设置rem以iphone7为标准  宽 375px  设置1rem = 10px
const setFontSize = ()=>{
  const htmlDom = document.getElementsByTagName('html')[0]
  let htmlWidth = document.documentElement.clientWidth || document.body.clientWidth
  if(htmlWidth >= 750) {
    htmlWidth = 750
  }
  if(htmlWidth <= 320) {
    htmlWidth = 320
  }
  htmlDom.style.fontSize = `${htmlWidth / 37.5}px`
}
window.onresize = setFontSize()
setFontSize()

